import pymysql

#version2.0

#阿斯卡垃圾得很付货款撒更大发卡机和

class D():

    def __init__(self, db):
        self.db = db

    def update(self, sql,params=[]):
        n = 0
        try:
            conn = pymysql.connect(user="root", password="root", host="127.0.0.1", port=3306, database=self.db)
            # 获取游标对象 执行sql
            cursor = conn.cursor()
            # 执行sql
            n = cursor.execute(sql,params)
        except Exception as  e:
            # 提交事务
            conn.commit()
        finally:
            # 释放资源
            cursor.close()
            conn.close()
        return n

    def selectOne(self,sql,q=[]):
        result = None
        try:
            conn = pymysql.connect(user="root", password="root", host="127.0.0.1", port=3306, database="test")
            # 获取游标对象，执行sql
            cursor = conn.cursor()
            # 执行sql
            cursor.execute(sql, q)
            result = cursor.fetchone()
        except Exception as e:
            print(e)
        finally:
            # 释放资源
            cursor.close()
            conn.cursor()
        return result

    def selectAll(self,sql,q=[]):
        result = None
        try :
            conn = pymysql.connect(user="root", password="root", host="127.0.0.1", port=3306, database="test")
            # 获取游标对象，执行sql
            cursor = conn.cursor()
            # 执行sql
            cursor.execute(sql, q)
            result = cursor.fetchall()
        except Exception as e:
            print(e)
        finally:
            # 释放资源
            cursor.close()
            conn.cursor()
        return result

    def selectMany(self,sql,n=1,q=[]):
        result = None
        try:
            conn = pymysql.connect(user="root", password="root", host="127.0.0.1", port=3306, database="test")
            # 获取游标对象，执行sql
            cursor = conn.cursor()
            # 执行sql
            cursor.execute(sql, q)
            result = cursor.fetchmany(n)
        except Exception as e:
            print(e)
        finally:
            # 释放资源
            cursor.close()
            conn.cursor()
        return result